//
//  PhoneViewController.swift
//  clreader
//
//  Created by colin on 2019/2/27.
//  Copyright © 2019 colin. All rights reserved.
//

import UIKit

/// 输入手机号
class PhoneViewController: CLBaseViewController {

    private lazy var textFieldPhone = UITextField(frame: CGRect.zero)

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white

        let buttonBack = UIButton(type: .custom).then {
            $0.setImage(UIImage(named: "button_back"), for: .normal)
            $0.addTarget(self, action: #selector(self.actionBack(_:)), for: .touchUpInside)
        }
        self.view.addSubview(buttonBack)
        buttonBack.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top)
            make.size.equalTo(CGSize(width: 44.0, height: 44.0))
        }

        // logo
        let imageViewLogo = UIImageView(image: UIImage(named: "logo")).then {
            $0.layer.cornerRadius = 20.0
            $0.layer.masksToBounds = true
        }
        self.view.addSubview(imageViewLogo)
        imageViewLogo.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 92.0, height: 92.0))
            make.centerX.equalToSuperview()
            make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(30.0)
        }
        // 阴影背景
        let imageViewLogoShadow = UIView().then {
            $0.backgroundColor = UIColor.white
            $0.layer.cornerRadius = 20.0
            $0.layer.shadowColor = UIColor.black.cgColor
            $0.layer.shadowOffset = CGSize(width:2.0,height:4.0)
            $0.layer.shadowOpacity = 0.3 //阴影
        }
        self.view.insertSubview(imageViewLogoShadow, belowSubview: imageViewLogo)
        imageViewLogoShadow.snp.makeConstraints({ (make) in
            make.edges.equalTo(imageViewLogo)
        })
        
        let labelPhone = UILabel().then {
            $0.text = "输入你的手机号"
            $0.textColor = UIColor(hex: "343434")
            $0.font = UIFont.systemFont(ofSize: 16.0, weight: .light)
        }
        self.view.addSubview(labelPhone)
        labelPhone.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(imageViewLogo.snp.bottom).offset(50.0)
        }
        
        let inputContainer = UIView().then {
            
            let labelAreaCode = UILabel().then {
                $0.text = "+86"
                $0.textColor = UIColor(hex: "848484")
                $0.font = UIFont.systemFont(ofSize: 19.0, weight: .regular)
            }
            $0.addSubview(labelAreaCode)
            labelAreaCode.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(10.0)
                make.bottom.equalToSuperview().offset(-8.0)
                make.width.equalTo(40.0)
            })
            
            self.textFieldPhone.do {
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 22.0, weight: .light)
                $0.placeholder = "请输入您的手机号"
                $0.keyboardType = .numberPad
                $0.clearButtonMode = .always
            }
            $0.addSubview(self.textFieldPhone)
            self.textFieldPhone.snp.makeConstraints { (make) in
                make.left.equalTo(labelAreaCode.snp.right).offset(30.0)
                make.top.equalToSuperview()
                make.bottom.equalToSuperview()
                make.right.equalToSuperview()
            }
            
            let linePhone = UIView()
            linePhone.backgroundColor = UIColor(hex: "F0F0F0")
            $0.addSubview(linePhone)
            linePhone.snp.makeConstraints { (make) in
                make.left.equalToSuperview()
                make.right.equalToSuperview()
                make.bottom.equalToSuperview()
                make.height.equalTo(0.5)
            }
        }
        self.view.addSubview(inputContainer)
        inputContainer.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(40.0)
            make.right.equalToSuperview().offset(-40.0)
            make.height.equalTo(44.0)
            make.top.equalTo(labelPhone.snp.bottom).offset(30.0)
        }
        
        // 登录按钮
        let buttonNext = UIButton(type: .custom).then {
            $0.backgroundColor = UIColor(hex: "AE1600")
            $0.layer.cornerRadius = 24.5
            $0.layer.masksToBounds = true
            $0.setTitle("下一步", for: .normal)
            $0.setTitleColor(UIColor.white, for: .normal)
            $0.titleLabel?.font = UIFont.systemFont(ofSize: 17.0, weight:.bold)
            $0.addTarget(self, action: #selector(self.actionNext(_:)), for: .touchUpInside)
        }
        self.view.addSubview(buttonNext)
        buttonNext.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20.0)
            make.right.equalToSuperview().offset(-20.0)
            make.top.equalTo(inputContainer.snp.bottom).offset(50.0)
            make.height.equalTo(49.0)
        }
        
    }

}

extension PhoneViewController {
    @objc private func actionBack(_ sender:Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc private func actionNext(_ sender:Any) {
        guard let phone = self.textFieldPhone.text?.trimmingCharacters(in: .whitespacesAndNewlines), !phone.isEmpty else {
            self.hud.showText("请输入手机号")
            return
        }
        
        self.hud.showProgress()
        _ = Router.sendSMS(phone: phone, valid_code_type: .registe).request { [weak self] (response:RequestResponse<JSONObject>) in
            
            self?.hud.hide()
            
            if case .success(_,let message) = response {
                self?.hud.showText(message)
                // 验证码发送成功
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
                    let controller = RegisterConfirmViewController(phone: phone)
                    self?.show(controller, sender: nil)
                })
            }
            
            if case .failure(_, let message) = response {
                self?.hud.showText(message)
            }
        }
        
    }
}
